package data

import (
	"encoding/json"
)

// a string slice
type SliceString struct {
	SlicerBase
}

// a cmp method for strings
func cmpString(i1 interface{}, i2 interface{}) int {
	if i1.(string) > i2.(string) {
		return 1
	} else if i1.(string) < i2.(string) {
		return -1
	} else {
		return 0
	}
}

// Builds a string slice.
func BuildSliceString(length, capacity int) SliceString {
	var v SliceString
	v.Init(length, capacity, cmpString)
	return v
}

// Builds a pointer to a string slice.
func NewSliceString(length, capacity int) *SliceString {
	var v SliceString
	v = BuildSliceString(length, capacity)
	return &v
}

// Returns the item at index.
func (this *SliceString) AtIndex(index int) string {
	var ifc interface{}
	ifc = this.SlicerBase.AtIndex(index)
	if ifc != nil {
		return ifc.(string)
	}
	return EmptyString
}

// Remove gets a string at index
// Deletes the string at index form the array
// Returns the string.
func (this *SliceString) Remove(index int) string {
	var ifc interface{}
	ifc = this.SlicerBase.Remove(index)
	if ifc != nil {
		return ifc.(string)
	}
	return EmptyString
}

// Appends a slice to the slice.
// Param src is the slice to be appended.
func (this *SliceString) AppendSlice(src []string) {
	var s string
	for _, s = range src {
		this.Append(s)
	}
}

// Returns the data as a slice.
func (this *SliceString) Slice() []string {
	var i, length int
	var item interface{}
	var ss []string
	length = len(this.Data)
	ss = make([]string, length, length)
	for i, item = range this.Data {
		ss[i] = item.(string)
	}
	return ss
}

// Marshals the data as an array.
func (this *SliceString) MarshalJSON() ([]byte, error) {
	return json.Marshal(this.Slice())
}
